﻿<%@ WebHandler Language="C#" Class="houseArchivement" %>

using System;
using System.Web;
using Chenxin.Lib.Utility;

public class houseArchivement :AjaxHandlerBase {

    override public void RequestProcess(HttpContext context)
    {
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";
        
        string op = context.Request.Params["op"];
        string strRet = "";
        try
        {
            switch (op)
            {
                case "Search":
                    strRet = SearchArchivement(context);
                    break;
            }
            context.Response.Write(strRet);
        }
        catch (Exception ex)
        {
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            context.Response.Write(strRet);
        }
    }

    //查找营业动态数据
    private string SearchArchivement(HttpContext context)
    {
        string strRet = "";
        string strParam;
        string ParentType;
        string ParentId;
        System.Collections.Hashtable htParams = new System.Collections.Hashtable();

        //前日
        if (context.Request.Params["StartDate"] == null && context.Request.Params["EndDate"] == null)
        {
            htParams.Add("StartDate", DateTime.Now.AddDays(-1).Date.ToShortDateString());
            htParams.Add("EndDate", DateTime.Now.AddDays(-1).Date.ToShortDateString());
        }
        //查询日期
        else
        {
            //开始日期
            strParam = context.Request.Params["StartDate"];
            if (!string.IsNullOrEmpty(strParam))
            {
                htParams.Add("StartDate", strParam);
            }
            //结束日期
            strParam = context.Request.Params["EndDate"];
            if (!string.IsNullOrEmpty(strParam))
            {
                htParams.Add("EndDate", strParam);
            }
        }
        //当前页数
        strParam = context.Request.Params["pageindex"];
        if (!string.IsNullOrEmpty(strParam))
        {
            htParams.Add("PageIndex", Convert.ToInt32(strParam) + 1);
        }
        //显示行数
        strParam = context.Request.Params["pagesize"];
        if (!string.IsNullOrEmpty(strParam))
        {
            htParams.Add("PageSize", strParam);
        }

        ParentType = context.Request.Params["ParentType"];
        ParentId = context.Request.Params["ParentId"];
        if (!string.IsNullOrEmpty(ParentType))
        {
            //int idxOfParent;
            string curParentType;
            string curParentId;
            string[] lsParent;

            lsParent = ParentType.Split(new char[] { '\\' });

            curParentType = lsParent[lsParent.Length - 1];

            lsParent = ParentId.Split(new char[] { '\\' });

            curParentId = lsParent[lsParent.Length - 1];
            
            htParams.Add(curParentType, curParentId);
        }
        //初期显示    
        else
        {
            //权限判断
            //全权限
            if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewAllArchivement))
            {
                htParams.Add("Permit", Constants.Permit_ViewAllArchivement);
            }
            //城市权限
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCityArchivement))
            {
                htParams.Add("Permit", Constants.Permit_ViewCityArchivement);
                htParams.Add("CurCityId", CurrentUserInfo.CityId);
            }
            //公司权限
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCompanyArchivement))
            {
                htParams.Add("Permit", Constants.Permit_ViewCompanyArchivement);
                htParams.Add("CurCompanyId", CurrentUserInfo.CompanyId);
            }
            //部门权限
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewDepartArchivement))
            {
                htParams.Add("Permit", Constants.Permit_ViewDepartArchivement);
                htParams.Add("CurUserId", CurrentUserInfo.UserId);
            }
            //个人权限
            else
            {
                htParams.Add("Permit", "PersonalPermit");
                htParams.Add("CurUserId", CurrentUserInfo.UserId);
            }
        }

        Chenxin.Lib.House.BLL.House_DataCollection bllDataCol = new Chenxin.Lib.House.BLL.House_DataCollection();

        System.Data.DataSet ds = bllDataCol.GetListByParams(htParams);

        strRet = retExecResult(Constants.ExecResult.Succ, "", ConvToClientModel(ds, ParentType, ParentId));
        return strRet;
    }

    //转化为Html
    private Constants.Pagination ConvToClientModel(System.Data.DataSet ds, string ParentType, string ParentId)
    {
        Constants.Pagination stuPag = new Constants.Pagination();
        System.Text.StringBuilder strHtml = new System.Text.StringBuilder();

        strHtml.Append("<table cellspacing=\"1\" class=\"tabsorternew\" id=\"report\">");
        strHtml.Append("	<thead>");
        strHtml.Append("		<tr style='white-space: nowrap;'>");
        strHtml.Append("            <th style='display:none' rowspan=2>类型ID</th>");
        strHtml.Append("            <th style='display:none' rowspan=2>类型</th>");
        strHtml.Append("            <th rowspan=2>操作</th>");
        strHtml.Append("            <th rowspan=2>类型</th>");
        strHtml.Append("			<th colspan=2>房源</th>");
        strHtml.Append("			<th rowspan=2>客户需求</th>");
        strHtml.Append("			<th colspan=2></th>");
        strHtml.Append("			<th colspan=3>需求</th>");
        strHtml.Append("		</tr>");
        strHtml.Append("		<tr>");
        strHtml.Append("			<th>出售</th>");
        strHtml.Append("			<th>出租</th>");
        strHtml.Append("			<th>跟单</th>");
        strHtml.Append("			<th>服务</th>");
        strHtml.Append("			<th>住宅</th>");
        strHtml.Append("			<th>商铺</th>");
        strHtml.Append("			<th>办公</th>");
        strHtml.Append("		</tr>");	
        strHtml.Append("	</thead>");
        strHtml.Append("	<tbody>");

        if (string.IsNullOrEmpty(ParentType) && ds.Tables[0].Rows.Count > 0)
        {
            strHtml.Append("		<tr>");
            strHtml.Append("			<td style='display:none'></td>");
            strHtml.Append("			<td style='display:none'></td>");
            strHtml.Append("            <td></td>");
            strHtml.AppendFormat("      <td>{0}</td>", "合计");
            strHtml.Append("			<td>");
            strHtml.Append("				<div>");
            strHtml.Append("					<ul>");
            strHtml.AppendFormat("					<li>有效:{0}条</li>", ds.Tables[0].Compute("Sum(ValidCount_Sale)", "1=1"));
            strHtml.AppendFormat("					<li>暂缓:{0}条</li>", ds.Tables[0].Compute("Sum(RespiteCount_Sale)", "1=1"));
            strHtml.AppendFormat("					<li>合计:{0}条</li>", Convert.ToInt32(ds.Tables[0].Compute("Sum(ValidCount_Sale)", "1=1")) + Convert.ToInt32(ds.Tables[0].Compute("Sum(RespiteCount_Sale)", "1=1")));
            strHtml.Append("					</ul>");
            strHtml.Append("				</div>");
            strHtml.Append("			</td>");
            strHtml.Append("			<td>");
            strHtml.Append("				<div>");
            strHtml.Append("					<ul>");
            strHtml.AppendFormat("					<li>有效:{0}条</li>", ds.Tables[0].Compute("Sum(ValidCount_Rent)", "1=1"));
            strHtml.AppendFormat("					<li>暂缓:{0}条</li>", ds.Tables[0].Compute("Sum(RespiteCount_Rent)", "1=1"));
            strHtml.AppendFormat("					<li>合计:{0}条</li>", Convert.ToInt32(ds.Tables[0].Compute("Sum(ValidCount_Rent)", "1=1")) + Convert.ToInt32(ds.Tables[0].Compute("Sum(RespiteCount_Rent)", "1=1")));
            strHtml.Append("					</ul>");
            strHtml.Append("				</div>");
            strHtml.Append("			</td>");
            strHtml.Append("			<td>");
            strHtml.Append("				<div>");
            strHtml.Append("					<ul>");
            //strHtml.AppendFormat("					<li>房源:{0}条</li>", ds.Tables[0].Compute("Sum(HouseCustomerCount)", "1=1"));
            strHtml.AppendFormat("					<li>合计:{0}条</li>", ds.Tables[0].Compute("Sum(DemandCustomerCount)", "1=1"));
            //strHtml.AppendFormat("					<li>合计:{0}条</li>", Convert.ToInt32(ds.Tables[0].Compute("Sum(HouseCustomerCount)", "1=1")) + Convert.ToInt32(ds.Tables[0].Compute("Sum(DemandCustomerCount)", "1=1")));
            strHtml.Append("					</ul>");
            strHtml.Append("				</div>");
            strHtml.Append("			</td>");
            strHtml.Append("			<td>");
            strHtml.Append("				<div>");
            strHtml.Append("					<ul>");
            strHtml.AppendFormat("					<li>房源:【买卖:{0}条‖租赁:{1}条‖租售:{2}条】</li>", ds.Tables[0].Compute("Sum(HouseTraceCount_Sale)", "1=1"),
                                                                                                               ds.Tables[0].Compute("Sum(HouseTraceCount_Rent)", "1=1"),
                                                                                                               ds.Tables[0].Compute("Sum(HouseTraceCount_SaleRent)", "1=1"));
            strHtml.AppendFormat("					<li>客源:【求租:{0}条‖求购:{1}条】</li>", ds.Tables[0].Compute("Sum(CustomerTraceCount_Rent)", "1=1"),
                                                                                                  ds.Tables[0].Compute("Sum(CustomerTraceCount_Sale)", "1=1"));
            strHtml.AppendFormat("					<li>系统房源:{0}条</li>", ds.Tables[0].Compute("Sum(SystemHouseTraceCount)", "1=1"));
            strHtml.AppendFormat("					<li>系统客源:{0}条</li>", ds.Tables[0].Compute("Sum(SystemCustomerTraceCount)", "1=1"));
            strHtml.AppendFormat("					<li>合计:{0}条</li>", Convert.ToInt32(ds.Tables[0].Compute("Sum(HouseTraceCount_Sale)", "1=1")) +
                                                                           Convert.ToInt32(ds.Tables[0].Compute("Sum(HouseTraceCount_Rent)", "1=1")) +
                                                                           Convert.ToInt32(ds.Tables[0].Compute("Sum(HouseTraceCount_SaleRent)", "1=1")) +
                                                                           Convert.ToInt32(ds.Tables[0].Compute("Sum(CustomerTraceCount_Rent)", "1=1")) +
                                                                           Convert.ToInt32(ds.Tables[0].Compute("Sum(CustomerTraceCount_Sale)", "1=1")) +
                                                                           Convert.ToInt32(ds.Tables[0].Compute("Sum(SystemHouseTraceCount)", "1=1")) +
                                                                           Convert.ToInt32(ds.Tables[0].Compute("Sum(SystemCustomerTraceCount)", "1=1")));
            strHtml.Append("					</ul>");
            strHtml.Append("				</div>");
            strHtml.Append("			</td>");
            strHtml.Append("			<td>");
            strHtml.Append("				<div>");
            strHtml.Append("					<ul>");
            strHtml.AppendFormat("					<li>带看:{0}条</li>", ds.Tables[0].Compute("Sum(ViewCount)", "1=1"));
            strHtml.AppendFormat("					<li>查看电话:{0}条</li>", ds.Tables[0].Compute("Sum(ViewPhoneCount)", "1=1"));
            strHtml.AppendFormat("					<li>备案:{0}条</li>", ds.Tables[0].Compute("Sum(MarkedCount)", "1=1"));
            strHtml.AppendFormat("					<li>钥匙:{0}条</li>", ds.Tables[0].Compute("Sum(KeyCount)", "1=1"));
            strHtml.Append("					</ul>");
            strHtml.Append("				</div>");
            strHtml.Append("			</td>");
            strHtml.Append("			<td>");
            strHtml.Append("				<div>");
            strHtml.Append("					<ul>");
            strHtml.AppendFormat("					<li>求购:{0}条</li>", ds.Tables[0].Compute("Sum(BuyCount_Flat)", "1=1"));
            strHtml.AppendFormat("					<li>求租:{0}条</li>", ds.Tables[0].Compute("Sum(RentCount_Flat)", "1=1"));
            strHtml.Append("					</ul>");
            strHtml.Append("				</div>");
            strHtml.Append("			</td>");
            strHtml.Append("			<td>");
            strHtml.Append("				<div>");
            strHtml.Append("					<ul>");
            strHtml.AppendFormat("					<li>求购:{0}条</li>", ds.Tables[0].Compute("Sum(BuyCount_Business)", "1=1"));
            strHtml.AppendFormat("					<li>求租:{0}条</li>", ds.Tables[0].Compute("Sum(RentCount_Business)", "1=1"));
            strHtml.Append("					</ul>");
            strHtml.Append("				</div>");
            strHtml.Append("			</td>");
            strHtml.Append("			<td>");
            strHtml.Append("				<div>");
            strHtml.Append("					<ul>");
            strHtml.AppendFormat("					<li>求购:{0}条</li>", ds.Tables[0].Compute("Sum(BuyCount_Office)", "1=1"));
            strHtml.AppendFormat("					<li>求租:{0}条</li>", ds.Tables[0].Compute("Sum(RentCount_Office)", "1=1"));
            strHtml.Append("					</ul>");
            strHtml.Append("				</div>");
            strHtml.Append("			</td>");
            strHtml.Append("        <tr><td colspan=12></td></tr>");
        }
        
        for (int idxRow = 0; idxRow < ds.Tables[0].Rows.Count; idxRow++)
        {
            strHtml.Append("<tr>");
            if (string.IsNullOrEmpty(ParentType))
            {
                strHtml.AppendFormat("<td style='display:none'>{0}</td>", ds.Tables[0].Rows[idxRow]["ObjectId"]);
                strHtml.AppendFormat("<td style='display:none'>{0}</td>", ds.Tables[0].Rows[idxRow]["ObjectType"]);
            }
            else
            {
                strHtml.AppendFormat("<td style='display:none'>{0}</td>", ParentId + @"\" + ds.Tables[0].Rows[idxRow]["ObjectId"].ToString());
                strHtml.AppendFormat("<td style='display:none'>{0}</td>", ParentType + @"\" + ds.Tables[0].Rows[idxRow]["ObjectType"].ToString());
            }
            if (ds.Tables[0].Rows[idxRow]["ObjectType"].ToString() != "UserId")
            {
                strHtml.AppendFormat("<td class='clsDown'>{0}</td>", "<div class='doimg' title='查看详细'></div>");
            }
            else
            {
                strHtml.Append("<td></td>");
            }

            strHtml.AppendFormat("<td>{0}</td>", ds.Tables[0].Rows[idxRow]["ObjectName"]);
            strHtml.Append("			<td>");
            strHtml.Append("				<div>");
            strHtml.Append("					<ul>");
            strHtml.AppendFormat("					<li>有效:{0}条</li>", ds.Tables[0].Rows[idxRow]["ValidCount_Sale"]);
            strHtml.AppendFormat("					<li>暂缓:{0}条</li>", ds.Tables[0].Rows[idxRow]["RespiteCount_Sale"]);
            strHtml.AppendFormat("					<li>合计:{0}条</li>", Convert.ToInt32(ds.Tables[0].Rows[idxRow]["ValidCount_Sale"]) + Convert.ToInt32(ds.Tables[0].Rows[idxRow]["RespiteCount_Sale"]));
            strHtml.Append("					</ul>");
            strHtml.Append("				</div>");
            strHtml.Append("			</td>");
            strHtml.Append("			<td>");
            strHtml.Append("				<div>");
            strHtml.Append("					<ul>");
            strHtml.AppendFormat("					<li>有效:{0}条</li>", ds.Tables[0].Rows[idxRow]["ValidCount_Rent"]);
            strHtml.AppendFormat("					<li>暂缓:{0}条</li>", ds.Tables[0].Rows[idxRow]["RespiteCount_Rent"]);
            strHtml.AppendFormat("					<li>合计:{0}条</li>", Convert.ToInt32(ds.Tables[0].Rows[idxRow]["ValidCount_Rent"]) + Convert.ToInt32(ds.Tables[0].Rows[idxRow]["RespiteCount_Rent"]));
            strHtml.Append("					</ul>");
            strHtml.Append("				</div>");
            strHtml.Append("			</td>");
            strHtml.Append("			<td>");
            strHtml.Append("				<div>");
            strHtml.Append("					<ul>");
            //strHtml.AppendFormat("					<li>房源:{0}条</li>", ds.Tables[0].Rows[idxRow]["HouseCustomerCount"]);
            strHtml.AppendFormat("					<li>合计:{0}条</li>", ds.Tables[0].Rows[idxRow]["DemandCustomerCount"]);
            //strHtml.AppendFormat("					<li>合计:{0}条</li>", Convert.ToInt32(ds.Tables[0].Rows[idxRow]["HouseCustomerCount"]) + Convert.ToInt32(ds.Tables[0].Rows[idxRow]["DemandCustomerCount"]));
            strHtml.Append("					</ul>");
            strHtml.Append("				</div>");
            strHtml.Append("			</td>");
            strHtml.Append("			<td>");
            strHtml.Append("				<div>");
            strHtml.Append("					<ul>");
            strHtml.AppendFormat("					<li>房源:【买卖:{0}条‖租赁:{1}条‖租售:{2}条】</li>", ds.Tables[0].Rows[idxRow]["HouseTraceCount_Sale"],
                                                                                                               ds.Tables[0].Rows[idxRow]["HouseTraceCount_Rent"],
                                                                                                               ds.Tables[0].Rows[idxRow]["HouseTraceCount_SaleRent"]);
            strHtml.AppendFormat("					<li>客源:【求租:{0}条‖求购:{1}条】</li>", ds.Tables[0].Rows[idxRow]["CustomerTraceCount_Rent"],
                                                                                                  ds.Tables[0].Rows[idxRow]["CustomerTraceCount_Sale"]);
            strHtml.AppendFormat("					<li>系统房源:{0}条</li>", ds.Tables[0].Rows[idxRow]["SystemHouseTraceCount"]);
            strHtml.AppendFormat("					<li>系统客源:{0}条</li>", ds.Tables[0].Rows[idxRow]["SystemCustomerTraceCount"]);
            strHtml.AppendFormat("					<li>合计:{0}条</li>", Convert.ToInt32(ds.Tables[0].Rows[idxRow]["HouseTraceCount_Sale"]) +
                                                                           Convert.ToInt32(ds.Tables[0].Rows[idxRow]["HouseTraceCount_Rent"]) +
                                                                           Convert.ToInt32(ds.Tables[0].Rows[idxRow]["HouseTraceCount_SaleRent"]) +
                                                                           Convert.ToInt32(ds.Tables[0].Rows[idxRow]["CustomerTraceCount_Rent"]) +
                                                                           Convert.ToInt32(ds.Tables[0].Rows[idxRow]["CustomerTraceCount_Sale"]) +
                                                                           Convert.ToInt32(ds.Tables[0].Rows[idxRow]["SystemHouseTraceCount"]) + 
                                                                           Convert.ToInt32(ds.Tables[0].Rows[idxRow]["SystemCustomerTraceCount"]));
            strHtml.Append("					</ul>");
            strHtml.Append("				</div>");
            strHtml.Append("			</td>");
            strHtml.Append("			<td>");
            strHtml.Append("				<div>");
            strHtml.Append("					<ul>");
            strHtml.AppendFormat("					<li>带看:{0}条</li>", ds.Tables[0].Rows[idxRow]["ViewCount"]);
            strHtml.AppendFormat("					<li>查看电话:{0}条</li>", ds.Tables[0].Rows[idxRow]["ViewPhoneCount"]);
            strHtml.AppendFormat("					<li>备案:{0}条</li>", ds.Tables[0].Rows[idxRow]["MarkedCount"]);
            strHtml.AppendFormat("					<li>钥匙:{0}条</li>", ds.Tables[0].Rows[idxRow]["KeyCount"]);
            strHtml.Append("					</ul>");
            strHtml.Append("				</div>");
            strHtml.Append("			</td>");
            strHtml.Append("			<td>");
            strHtml.Append("				<div>");
            strHtml.Append("					<ul>");
            strHtml.AppendFormat("					<li>求购:{0}条</li>", ds.Tables[0].Rows[idxRow]["BuyCount_Flat"]);
            strHtml.AppendFormat("					<li>求租:{0}条</li>", ds.Tables[0].Rows[idxRow]["RentCount_Flat"]);
            strHtml.Append("					</ul>");
            strHtml.Append("				</div>");
            strHtml.Append("			</td>");
            strHtml.Append("			<td>");
            strHtml.Append("				<div>");
            strHtml.Append("					<ul>");
            strHtml.AppendFormat("					<li>求购:{0}条</li>", ds.Tables[0].Rows[idxRow]["BuyCount_Business"]);
            strHtml.AppendFormat("					<li>求租:{0}条</li>", ds.Tables[0].Rows[idxRow]["RentCount_Business"]);
            strHtml.Append("					</ul>");
            strHtml.Append("				</div>");
            strHtml.Append("			</td>");
            strHtml.Append("			<td>");
            strHtml.Append("				<div>");
            strHtml.Append("					<ul>");
            strHtml.AppendFormat("					<li>求购:{0}条</li>", ds.Tables[0].Rows[idxRow]["BuyCount_Office"]);
            strHtml.AppendFormat("					<li>求租:{0}条</li>", ds.Tables[0].Rows[idxRow]["RentCount_Office"]);
            strHtml.Append("					</ul>");
            strHtml.Append("				</div>");
            strHtml.Append("			</td>");     
            strHtml.Append("</tr>");
        }
        strHtml.Append("	</tbody>");
        strHtml.Append("</table>");

        stuPag.count = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
        stuPag.data = strHtml.ToString();
        return stuPag;
    }
}